//index.js
//获取应用实例
const app = getApp()
var template = require('../../template/template.js');
var request = require('../../utils/https.js')
var uri = 'index/deal_index.html'
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    goodsList:[],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    page:1,
    pageSize: 10,
    refreshTime: '', // 刷新的时间 
    allPages: '',    // 总页数
    loadtext: '加载更多……',
    hideBottom: true
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    template.tabbar("tabBar", 0, this);
    wx.showNavigationBarLoading();
    this.getList();
    setTimeout(function () {
      wx.hideNavigationBarLoading()
    }, 2000);
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo;
          console.log(res);
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  getList: function () {
    var that = this;
    var pageIndex = that.data.page;
    request.req(uri, {
      user_id: wx.getStorageSync('userId'),
      page: that.data.page, page_size: that.data.pageSize
    },
      (err, res) => {
        if (res.data.status) {
          var list = [];
          if (res.data.data != '没有数据') {
            for (var i = 0; i < res.data.data.length; i++) {
              list.push(res.data.data[i]);
            }
            setTimeout(function () {
              that.setData({
                hideBottom: true,
                loadingText: '加载中...'
              })
            }, 2000)
          } else {
            that.setData({
              hideBottom: false,
              loadingText: '已无更多数据'
            })
          }
          if (that.data.page == 1) {
            that.setData({
              allPages: res.data.count,
              goodsList: list,
              hideBottom: true,
            })
          } else {
            var nowList = that.data.goodsList;
            nowList = nowList.concat(list);
            that.setData({
              allPages: res.data.count,
              goodsList: nowList,
              hideBottom: false,
              loadingText: '加载中...',
            })
            setTimeout(function () {
              that.setData({
                hideBottom: true,
                loadingText: '加载中...'
              })
            }, 2000)
          }
        }else{
          wx.navigateTo({
            url: '../login/login'
          })
        }
      })
  },
  onPullDownRefresh: function () {
    var that = this;
    this.setData({
      page: 1
    })
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    this.setData({
      page: that.data.page + 1
    })
    this.getList();
  },
})
